#ifndef CONNECTIONITEM_H
#define CONNECTIONITEM_H

#include <QGraphicsLineItem>
#include <QPen>

class PlcElement;

class ConnectLine : public QGraphicsLineItem
{
public:
    enum { Type = UserType + 10 };
    enum Side
    {
        LEFT,
        RIGHT
    };
    // 元素-元素构造函数
    ConnectLine(PlcElement *startItem, Side startSide, PlcElement *endItem,   Side endSide, QGraphicsItem *parent = nullptr);
    // 元素-锚点构造函数
    ConnectLine(PlcElement *startItem, Side startSide, const QPointF& endAnchorScenePos, QGraphicsItem *parent = nullptr);

    int type() const override;
    // 折线模式设置（start -> bend -> end）
    void setPolylineMode(bool on, const QPointF& bendPoint);
    void updatePosition();
    PlcElement *startItem() const;
    PlcElement *endItem() const;
    Side startSide() const;
    Side endSide() const;

    // 序列化所需的只读 getter
    bool isPolyline() const;
    QPointF bendPoint() const;
    QPointF startAnchorScenePos() const;
    QPointF endAnchorScenePos() const;

    bool startIsAnchor();
    bool endIsAnchor();
    void drawConnection(QPainter *painter) const;

protected:
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
    QPainterPath shape() const override;
    QRectF boundingRect() const override;

private:
    PlcElement *startItem_;
    PlcElement *endItem_;
    QColor color_ = Qt::black;
    Side startSide_ = RIGHT;
    Side endSide_ = LEFT;

    // 支持锚点（场景坐标固定点）
    bool startIsAnchor_ = false;
    bool endIsAnchor_ = false;
    QPointF startAnchorScenePos_;
    QPointF endAnchorScenePos_;

    // 折线显示支持
    bool polyline_ = false;
    QPointF bend_;   // 拐点（场景坐标）
};

#endif // CONNECTIONITEM_H
